let lastSR = null;
let lastR = null;
let combinTimer = null;
let forceInterval = null;

function consoleAddS (parent, str) {
    /* clear the timer of `new receive entry` */
    if (combinTimer != null) {
        clearTimeout(combinTimer);
    }
    combinTimer = setTimeout(() => {    /* if time out, means that the following received message needn't combin */
        combinTimer = null;
    }, ocom.console.combinTime);
    /* create the dom of `msg-sr` */
    let domSR = document.createElement('div');
    domSR.innerHTML = `<div>${str}</div>`;
    domSR.className = 'msg-sr';
    /* mark the dom as parent, so that the following received message can combine to it */
    lastSR = domSR;
    parent.appendChild(domSR);
    /* scroll control */
    consoleScroll(parent);
}

function consoleAddR (parent, str, isblock=true) {
    if (forceInterval != null) {
        clearInterval(forceInterval);
        removeAndAppend();
    }
    if (isblock) { lastSR = null; }
    let domR = document.createElement('div');
    domR.innerHTML = str;
    domR.className = 'msg-r';
    parent.appendChild(domR);
    /* scroll control */
    consoleScroll(parent);
    return domR;
}

function consoleAppendS (str) { /* local function */
    let domR = document.createElement('div');
    domR.innerHTML = str;
    lastSR.appendChild(domR);
    /* scroll control */
    consoleScroll(ocom.console.doms.consoleMsgs);
}

function removeAndAppend () {   /* local function */
    if (lastR != null && lastSR != null) {
        consoleAppendS(lastR.innerHTML);
        lastR.parentNode.removeChild(lastR);
        lastR = null;
    }
}

function consoleAutoR (parent, str) {
    /* check whether the combin timer is out, combin the new received message if timer not out */
    if (combinTimer == null) {
        if (forceInterval != null) {
            clearInterval(forceInterval);
            removeAndAppend();
        }
        lastR = consoleAddR(parent, str, false);
        /* do force combin by the config */
        if (lastSR != null && ocom.console.forceCombin) {
            let cnt = 1;
            forceInterval = setInterval(() => {
                /* 7 (8-1) from the margin of `--msg-margin-top` */
                if (cnt < 8 && lastR != null) {
                    lastR.style.transform = `translateY(-${cnt}px)`;
                    cnt ++;
                } else {
                    removeAndAppend();
                    clearInterval(forceInterval);
                    forceInterval = null;
                }
            }, 50);
        }
    } else {
        if (lastSR == null) {
            consoleAddR(parent, str);
        } else {
            /* restart the combin timer */
            clearTimeout(combinTimer);
            combinTimer = setTimeout(() => {
                combinTimer = null;
            }, ocom.console.combinTime);
            /* append to the send message box */
            consoleAppendS(str);
        }
    }
}

function consoleScroll (parent) {   /* local function */
    parent.scrollTop = parent.scrollHeight;
}